namespace Stee.CAP8.Communication.GrpcInterface
{
    using System.Collections.Generic;
    using Microsoft.Extensions.Configuration;

    /// <summary>
    /// To build the configuration for Grpc Client
    /// </summary>
    public class GrpcClientOptionBuilder
    {
        public GrpcClientOption CurrentOption { get; }

        /// <summary>
        /// By default, initialize the Grpc Client according to JSON file
        /// </summary>
        /// <param name="configuration"></param>
        public GrpcClientOptionBuilder(IConfiguration configuration)
        {
            CurrentOption = new GrpcClientOption();
            if (configuration != null)
            {
                configuration.GetSection("GrpcClient").Bind(CurrentOption);                
            }
        }
        /// <summary>
        /// Set the Server Address for Grpc Service 
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public GrpcClientOptionBuilder SetServerAddress(string addr)
        {
            CurrentOption.ServerAddr = addr;
            return this;
        }

        /// <summary>
        /// Set the Https Cert Check type for Grpc Service 
        /// </summary>
        /// <param name="certCheckType"></param>
        /// <returns></returns>
        public GrpcClientOptionBuilder SetCertCheckType(CertCheckType certCheckType)
        {
            CurrentOption.HttpsCertCheck = certCheckType;
            return this;
        }

        /// <summary>
        /// Set the Cert Issuer for Grpc Service 
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public GrpcClientOptionBuilder SetCertIssuer(string certIssuer)
        {
            CurrentOption.CertIssuer = certIssuer;
            return this;
        }

        /// <summary>
        /// Once get the token from Identiy sever, should set the token for anthentication
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public GrpcClientOptionBuilder SetToken(string token)
        {
            GrpcClientOption.Token = token;
            return this;
        }
        /// <summary>
        /// Set the Grpc Client Type for Auto DI by manual
        /// </summary>
        /// <param name="info">Which Grpc Client you want to add to DI container</param>
        /// <returns></returns>
        public GrpcClientOptionBuilder AddGrpcClientConfig(GrpcClientInfo info)
        {
            if (CurrentOption.GrpcClients == null)
            {
                CurrentOption.GrpcClients = new List<GrpcClientInfo>();
            }
            if (info != null)
            {
                CurrentOption.GrpcClients.Add(info);
            }            
            return this;
        }
        /// <summary>
        /// Set which Grpc Client you want to add the DI container
        /// </summary>
        /// <param name="clientInfos"></param>
        /// <returns></returns>
        public GrpcClientOptionBuilder SetGrpcClientConfig(List<GrpcClientInfo> clientInfos)
        {
            CurrentOption.GrpcClients = clientInfos;
            return this;
        }
    }
}
